package sms_interface;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;


public class SendSMS {

	/**
	 * @param args
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws InterruptedException {
		while(true)
		{
		try
		{
            Class.forName("com.mysql.jdbc.Driver");
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }

        try
        {
            // DSN name --> IVR
            String url="jdbc:mysql://localhost:3306/ivr";
            Connection con=DriverManager.getConnection(url,"ivr","ivr");
            Statement S = con.createStatement();
            ResultSet rs = S.executeQuery("Select * from sms_sent where sent = 'n'");
            while(rs.next())
            {
            	String number = "0"+rs.getString(2).substring(0, 10);
            	String message = rs.getString(3);
            	int id = rs.getInt(5);
            	System.out.println(number + "--->>" + message + "--->>" + id);
            	send_sms(number,message);
            	String X = "sms-send.html?number="+number+"&smsencode= "+message+"&send=Send";
            	System.out.println(X);
            	
            	
            	Statement upd = con.createStatement();
            	upd.executeUpdate("Update sms_sent set sent_time = now() ,sent = 'y' where id = "+id);
            }
        }
        catch(Exception exp)
        {
        	exp.printStackTrace();
        }
        Thread.sleep(30000);
		}
	}
	
	public static void send_sms(String number, String messages) throws InterruptedException
	{
		String arr[] =messages.split("\\s");
		String reformed ="";
		for(int i = 0;i<arr.length;i++)
		{
			reformed = reformed +"%20" + arr[i];
		}
		try {
			Runtime.getRuntime().exec("wget -o smssent -r --no-check-certificate https://admin:99admin11@192.168.1.175/admin/forms/sms-send.html?number="+number.toString()+"&smsencode="+reformed+"&send=Send").waitFor();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}



